package com.probox.modules.controller.platfom;

import cn.hutool.core.util.ObjectUtil;
import com.probox.common.core.text.Convert;
import com.probox.common.core.utils.poi.ExcelUtil;
import com.probox.common.core.web.controller.BaseController;
import com.probox.common.core.web.domain.AjaxResult;
import com.probox.common.core.web.page.TableDataInfo;
import com.probox.common.log.annotation.Log;
import com.probox.common.log.enums.BusinessType;
import com.probox.common.security.annotation.PreAuthorize;
import com.probox.common.entity.domain.platfom.PlatformAddress;
import com.probox.common.entity.service.platfom.IPlatformAddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 店铺地址Controller
 *
 * @author usoft
 * @date 2021-07-05
 */
@RestController
@RequestMapping("/platform/address")
public class PlatformAddressController extends BaseController {
    @Autowired
    private IPlatformAddressService platformAddressService;

    /**
     * 查询店铺地址列表
     */
    @PreAuthorize(hasPermi = "platform:address:list")
    @GetMapping("/list")
    public TableDataInfo list(PlatformAddress platformAddress) {
        startPage();
        List<PlatformAddress> list = platformAddressService.queryList(platformAddress);
        return getDataTable(list);
    }

    /**
     * 导出店铺地址列表
     */
    @PreAuthorize(hasPermi = "platform:address:export")
    @Log(title = "店铺地址", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PlatformAddress platformAddress) throws IOException {
        List<PlatformAddress> list = platformAddressService.queryList(platformAddress);
        ExcelUtil<PlatformAddress> util = new ExcelUtil<PlatformAddress>(PlatformAddress.class);
        util.exportExcel(response, list, "店铺地址数据");
    }

    /**
     * 获取店铺地址详细信息
     */
    @PreAuthorize(hasPermi = "platform:address:query")
    @GetMapping(value = "/{platformAddressUid}")
    public AjaxResult getInfo(@PathVariable("platformAddressUid") Long platformAddressUid) {
        PlatformAddress platformAddress = platformAddressService.selectPlatformAddressById(platformAddressUid);
        AjaxResult ax = new AjaxResult();
        ax.put("data",platformAddress);
        if(ObjectUtil.isNotEmpty(platformAddress)){
            Long [] axs = new Long[3];
            axs[0] = Convert.toLong(platformAddress.getProvinceCode());
            axs[1] =Convert.toLong(platformAddress.getCityCode());
            axs[2] =Convert.toLong(platformAddress.getCountyCode());
            ax.put("codds",axs);
        }
        return ax;
    }

    /**
     * 新增店铺地址
     */
    @PreAuthorize(hasPermi = "platform:address:add")
    @Log(title = "店铺地址", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PlatformAddress platformAddress) {
        return toAjax(platformAddressService.insertPlatformAddress(platformAddress));
    }

    /**
     * 修改店铺地址
     */
    @PreAuthorize(hasPermi = "platform:address:edit")
    @Log(title = "店铺地址", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PlatformAddress platformAddress) {
        return toAjax(platformAddressService.updatePlatformAddress(platformAddress));
    }

    /**
     * 删除店铺地址
     */
    @PreAuthorize(hasPermi = "platform:address:remove")
    @Log(title = "店铺地址", businessType = BusinessType.DELETE)
    @DeleteMapping("/{platformAddressUids}")
    public AjaxResult remove(@PathVariable Long[] platformAddressUids) {
        return toAjax(platformAddressService.deletePlatformAddressByIds(platformAddressUids));
    }
}
